﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;
using VkVideoPlayer.UI.Services;
using VkontakteCore;


namespace VkVideoPlayer.UI.ViewModels
{
	public class MainViewModel : ViewModelBase
	{
		public MainViewModel()
		{
		}

		private VideoViewModel _videoViewModel;

		public VideoViewModel VideoModel
		{
			get { return _videoViewModel; }
			set
			{
				_videoViewModel = value;
				OnPropertyChange("VideoModel");
			}
		}

		public override void OnNavigatedFrom(PhoneApplicationPage page, NavigationEventArgs e)
		{
			base.OnNavigatedFrom(page, e);

		}

		public override void OnNavigatedTo(PhoneApplicationPage page, NavigationEventArgs e)
		{
			base.OnNavigatedTo(page, e);
			if (VideoModel == null)
			{
				GetService<IVkontakteApi>().GetCurrentUserProfile((user) =>
				                {
				                    User = user;
				                	GetService<IVkontakteApi>().GetUserVideos(user.Uid, "0" /*offset*/, "20" /*count*/,
				                	                                          (videos) =>
				                	                                          	{
				                	                                          		VideoModel = new VideoViewModel
				                	                                          		             	{User = user, Videos = videos};
				                	                                          	},
				                	                                          (error) =>
				                	                                          	{
				                	                                          		GetService<IErrorHandlerService>().HandleError(error);
				                	                                          	});
				                },
				                (error) => GetService<IErrorHandlerService>().HandleError(error));
			}
			else
			{
				
			}
		}
	}
}